import _ from 'lodash';
import moment from 'moment';

export const authTypes = [
  { name: '授权码模式', value: 'authorization_code' },
  { name: '简化模式', value: 'implicit' },
  { name: '密码模式', value: 'password' },
  { name: '客户端模式', value: 'client_credentials' },
  { name: '刷新模式', value: 'refresh_token' },
];

export function getOAuthTypeNames(str) {
  if (!str) return [];
  const values = str.split(',');
  return values.map(value => _.find(authTypes, t => t.value === value).name);
}

export function getResources(str) {
  if (!str) return [];
  return str.split(',');
}

export function humanizeTime(value) {
  let timeText = '';
  if (value) {
    timeText = moment.duration(Number(value), 'seconds').humanize();
  } else {
    timeText = '未设置';
  }
  return timeText;
}
